package de.tum.in.eist.im.client.GUIwidgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

import de.tum.in.eist.im.client.GUI;
import de.tum.in.eist.im.shared.User;

public class LoginDialog extends GUIwidget{
	private DialogBox loginDialogBox = new DialogBox();

	private AbsolutePanel loginPanel = new AbsolutePanel();

	private Label lblUsername = new Label("Username");
	private Label lblPassword = new Label("Password");
	private Button loginButton = new Button("Login");
	private Button registerButton = new Button("Register");
	private TextBox usernameBox = new TextBox();
	private PasswordTextBox passwordBox = new PasswordTextBox();

	public LoginDialog(GUI parent) {
		setGUIandRootPanel(parent);
		
		usernameBox.setSize("155px", "16px");
		usernameBox.setMaxLength(20);
		passwordBox.setSize("155px", "16px");

		loginPanel.setSize("400px", "100px");
		loginPanel.add(lblUsername, 10, 10);
		loginPanel.add(usernameBox, 72, 10);
		loginPanel.add(lblPassword, 10, 48);
		loginPanel.add(passwordBox, 72, 48);
		loginPanel.add(loginButton, 243, 48);
		loginPanel.add(registerButton, 296, 48);

		loginDialogBox.setModal(true);
		loginDialogBox.add(loginPanel);

		addHandlersToWidgets();
	}

	public void addHandlersToWidgets() {
		class ChatDialogHandler implements ClickHandler, KeyUpHandler {
			public void onClick(ClickEvent event) {
				submitInput();
			}

			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					submitInput();
				}
			}

			public void submitInput() {
				parentGUI.getParentIM().login(getUserFromInput());
			}
		}

		ChatDialogHandler dialogHandler = new ChatDialogHandler();
		loginButton.addClickHandler(dialogHandler);
		passwordBox.addKeyUpHandler(dialogHandler);

		registerButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				parentGUI.getParentIM().register(getUserFromInput());
			}
		});
	}

	public void showDialog() {
		loginDialogBox.setPopupPosition(10, 10);
		loginDialogBox.show();
	}

	public void hideDialog() {
		loginDialogBox.hide();
	}

	public User getUserFromInput() {
		String username = usernameBox.getText();
		String password = passwordBox.getText();
		User newUser = new User(username, password);
		return newUser;
	}

	public void flagAsInvalidInput() {
		usernameBox.selectAll();
		passwordBox.selectAll();
		Window.alert("Are you registered yet? If so, please check your input!");
		usernameBox.setFocus(true);
	}
}
